<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\ImageService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class ImageController extends BaseController
{
    //文件

    //列表
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new ImageService())->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //保存
    public function upload(Request $request)
    {
        $img = $request->post('img', '');
        if (!$img) {
            return $this->toJson(-2, '图片必须的');
        }
        $fpath = Upload::getPathByBase64($img);
        //ddd($img, $fpath);
        //$fpath = env('DOMAIN') . '/' . $fpath;
        $sizes = getimagesize($img);
        $res = (new ImageService())->addOne($fpath, $sizes[0], $sizes[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $arys = [
            'domain' => env('DOMAIN') . '/',
            'img' => $fpath,
            'w' => $sizes[0],
            'h' => $sizes[1],
        ];
        return $this->toJson(0, '', $arys);
    }

    //保存
    public function uploadVideo(Request $request)
    {
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        //$img = $request->file('video', '');
        //if (!$img) {
        //    return $this->toJson(-2, '视频必须的');
        //}
        $name = time();
        $fpath = Upload::uploadVideo($request, 1, 1, $name);
        if ($fpath['code'] < 0) {
            return $this->toJson(-2, $fpath['msg']);
        }
        //ddd($img, $fpath);
        //$fpath = env('DOMAIN') . '/' . $fpath;
        return $this->toJson(0, '', ['fpath' => $fpath['data']]);
    }
}
